package 数据结构.树.线段树;

import java.util.Arrays;

/**
 * @author Veng Su 1344114844@qq.com
 * @date 2018/7/26 13:14
 */
public class SegmentTree<E> {
    private E[] tree;
    private E[] data;
    private Merger<E > merger;

    public SegmentTree(E[] arr, Merger<E> merger) {
        this.data = (E[]) new Object[arr.length];
        this.merger=merger;
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
        }
        tree = (E[]) new Object[4 * arr.length];
        buildSegmentTree(0, 0, data.length - 1);

    }

    /**
     * 创建l...r之间的的线段树
     *
     * @param treeIndex
     * @param l
     * @param r
     */
    private void buildSegmentTree(int treeIndex, int l, int r) {
        if (l == r) {
            tree[treeIndex] = data[l];
            return;
        }
        int leftTreeIndex=leftChild(treeIndex);
        int rightTreeIndex=rightChild(treeIndex);
        int mid=l+(r-l)/2;
        buildSegmentTree(leftTreeIndex,l,mid);
        buildSegmentTree(rightTreeIndex,mid+1,r);
        //这里的线段树根据业务变更，我用做求和
        tree[treeIndex]=merger.merge(tree[leftTreeIndex],tree[rightTreeIndex]);
    }

    public int getSize() {
        return data.length;
    }

    public E get(int index) {
        if (index < 0 || index >= data.length) {
            throw new IllegalArgumentException("数组越界");
        }
        return data[index];
    }

    private int leftChild(int index) {
        return 2 * index + 1;
    }

    private int rightChild(int index) {
        return 2 * index + 2;
    }



    public E query(int queryL,int queryR){
        if (queryL<0||queryL>=data.length||queryR<0||queryR>=data.length||queryL>queryR){
            throw new IllegalArgumentException("数组越界");
        }

        return query(0,0,data.length-1,queryL,queryR);
    }
    //查找l...r的范围内，搜索queryL...queryR的范围的值
    private E query(int treeIndex,int l ,int r,int queryL,int queryR){

        if (l==queryL&&r==queryR){
            return tree[treeIndex];
        }
        int mid=l+(r-l)/2;
        int leftChild=leftChild(treeIndex);
        int rightChild=rightChild(treeIndex);
        if (queryL>=mid+1){
            return query(rightChild,mid+1,r,queryL,queryR);
        }else if(queryR<=mid){
            return query(leftChild,l,mid,queryL,queryR);
        }else {
            E left=query(leftChild,l,mid,queryL,mid);
            E right=query(rightChild,mid+1,r,mid+1,queryR);
            return merger.merge(left,right);
        }
    }

    /**
     * 将index位置的值更新为e
     * @param index
     * @param e
     */
    public void set( int index,E e){
        if (index<0||index>=data.length){
            throw new IllegalArgumentException("数组越界");
        }
        data[index]=e;
        set(0,0,data.length-1,index,e);
    }
    private void set(int treeIndex,int l,int r,int index,E e){
        if (l==r){
            tree[treeIndex]=e;
            return;
        }
        int mid=l+(r-l)/2;
        int leftChild=leftChild(treeIndex);
        int rightChild=rightChild(treeIndex);
        if (index>=mid+1){
            set(rightChild,mid+1,r,index,e);
        }else if (index<=mid){
            set(leftChild,0,mid,index,e);
        }
        tree[treeIndex]=merger.merge(tree[leftChild],tree[rightChild]);
    }
    @Override
    public String toString() {
        return "SegmentTree{" +
                "tree=" + Arrays.toString(tree) +
                ", data=" + Arrays.toString(data) +
                ", merger=" + merger +
                '}';
    }
}
